/**
 * Created by jocky on 2017/6/3.
 */

//绑定enter事件
$('body').bind('keyup', function (event) {
    if (event.keyCode == "13") {
        $("#loginBtn").trigger("click");
    }
});

/**
 * 点击获取验证码
 * @param type
 */
function getNewCaptcha(type) {
    var url;
    if (type == 1) {
        url = '/reg/captcha';
    } else if (type == 2) {
        url = '/login/captcha';
    } else {
        url = '/password/captcha';
    }
    $.getJSON(url, {refresh: 1}, function (json) {
        $("#captchaImg" + type)[0].src = json.url;
    });
}

var notify;
function valid() {
    var form = $("#login");

    var validFlag = true;
    $.each(form.find('input'), function (i, v) {
        if (v.name && !$.trim($(v).val())) {
            setNotify($(v).attr('data-error'), true);
            $(v).focus();
            validFlag = false;
            return false;
        }
    });

    return validFlag;
}

function setNotify(msg, close) {
    if (close) {
        PNotify.removeAll();
    }

    new PNotify({
        title: '提示',
        text: msg,
        type: 'error',
        styling: 'bootstrap3',
        delay: 5000
    });
}

function sendLogin() {
    if (!valid()) return false;

    var params = {};
    $.each($("#login").find('input'), function (i, v) {
        if (v.name && $.trim($(v).val())) {
            if (v.name != 'user_agreement') params[v.name] = $.trim($(v).val());
        }
    });

    $.post('/login/submit', params, function (res) {
        var json = JSON.parse(res);
        if (json.code != 1) {
            //重新获取一次验证码
            getNewCaptcha(2);
            //错误提醒
            setNotify(json.msg, true);
        } else {
            window.location.href = '/#/trader/lists';
        }
    })
}
function getUserAgreement() {
    $("#modal_pop").modal('show');
}
